<?php
if(@$_POST["send"]=="action"){
   		
	$img_dir = '';
	$np = $_POST['noi_up'];
	if($np == 'san_pham')
	{
		$img_dir = 'category/';
		$sp = 'selected="selected"';
		
	}
	elseif ($np == 'du_an')
	{
		$img_dir = 'project/';
		$da = 'selected="selected"';
	}
	elseif ($np == 'gioi_thieu')
	{
		$img_dir = 'img/';
		$gt = 'selected="selected"';
	}
	else
	{
		$img_dir = 'img/';
		$dv = 'selected="selected"';
	}
    $uploaddir = $img_dir; 
    $ext = explode('.',$_FILES['userfile']['name']);
    if($ext[1]=='gif' || $ext[1]=='GIF'|| $ext[1]=='jpg' || $ext[1]=='JPG'){
        $id=rand();
        $filename=$id.'.'.$ext[1];
        $uploadfile = $uploaddir . $filename;
		$size=round(filesize($_FILES['userfile']['tmp_name'])/1000,2);
    
			 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
                
                
                echo "<script>showAlert('Thông báo','Upload file thành công!','');</script>"; 
				echo 'Link file ảnh vừa upload: <font color="#FF0000">'.$uploadfile.'</font></br>';
				echo '<i>(Dung lượng file upload: '.$size.' Kb)</i>';
                
            } 
            else { 
                echo "<script>showAlert('Lỗi','Có lỗi xảy ra!','');</script>"; 
            } 
    }
    else {
        echo "<script>showAlert('Lỗi','File ảnh không đúng chuẩn (jpg,jpeg,gif)!','');</script>";
    } 
	
}

?>

   

 
<form name="upload" action="admin_master_page.php?cmd=10" method="POST" enctype="multipart/form-data" >
<p>
Nơi upload ảnh:
<select name="noi_up">
	<option <?php echo @$sp; ?> value="san_pham">Sản phẩm</option>
    <option <?php echo @$da; ?> value="du_an">Dự án</option>
    <option <?php echo @$gt; ?> value="gioi_thieu">Giới thiệu</option>
    <option <?php echo @$dv; ?> value="dich_vu">Dịch vụ</option>
</select>

</p> 
<p>Chọn file upload: <input type="file" name="userfile"> </p>
<input type="hidden" name="send" value="action" />

<input type="submit" name="upload" value="Upload"> 
</form> 

<p class="group-line">
   <span class="group-title">Danh mục hình ảnh</span>
</p>


    <?php
	
	$n = 0;
	foreach (glob("category/*.*") as $filename)
	{
		$pics_arry[$n] = $filename;
		$n++;
	}
	foreach (glob("project/*.*") as $filename)
	{
		$pics_arry[$n] = $filename;
		$n++;
			
	}
	

	@$act = $_GET['act'];
	if($act == 'del')
	{
		$path = $_GET['path'];
		unlink($path);
		echo "<script>";
			echo "window.location = 'admin_master_page.php?cmd=10'";
		echo "</script>";
	}
	//phan trang------------------------------
	//echo $pics_arry[10].'/'.count($pics_arry);
	
	@$g_func = new global_function();
	@$cur_page = $_GET['page'];
	$ROWS_PER_PAGE = 10;
	$total_record = count($pics_arry);
	$begin = $g_func->get_begin($cur_page, $ROWS_PER_PAGE, $total_record);
	$dk = 0;
	if($begin+$ROWS_PER_PAGE < $total_record)
	{
		$dk = $begin+$ROWS_PER_PAGE;
	}
	else
	{
		$dk = $total_record;
	}
?>
<table width="680px" class="table-list">
	<tr class="table-header">
        <td width="50px">STT</td>
        <td >Đường dẫn ảnh</td>
        <td width="65px">Ảnh</td>
        <td width="45px">Xóa</td>
    </tr>
<?php
	$stt = 0;
	for($i = $begin ; $i < $dk ; $i++)
	{
		$stt = $i + 1;
		echo '<tr>';
		echo '<td align="center">'.$stt.'</td>';
		echo '<td>'.$pics_arry[$i].'</td>';
		echo '<td align="center"><img src="'.$pics_arry[$i].'" width = "50px" height ="40px" /></td>';
		echo '<td align="center"><img src="img/ico_delete.png" onclick="showConfirm(\'Xác nhận\', \'Bạn có chắc muốn xóa không?\', \'admin_master_page.php?cmd=10&act=del&path='.$pics_arry[$i].'\')" class="del" /></td>';
		//echo $pics_arry[$i].'</br>';
		echo '</tr>';
	}
	
?>
</table>
<?php
	$g_func->admin_paging($total_record,$ROWS_PER_PAGE);
?>
